package com.ezcoding.dao;

import com.ezcoding.model.UserRelation;
import com.jfinal.plugin.activerecord.Db;

import java.util.List;

public class UserRelationDao {

    public static UserRelationDao dao = new UserRelationDao();

    public List<UserRelation> getClientsByUsername(String username) {
        String sql = Db.getSql("UserRelation.getClientsByUsername");

        return UserRelation.dao.find(sql, username);
    }

    public List<UserRelation> getGiversByUsername(String username) {
        String sql = Db.getSql("UserRelation.getGiversByUsername");

        return UserRelation.dao.find(sql, username);
    }

    public List<UserRelation> getUserRelationsBySourceUsername(String sourceUsername) {
        String sql = Db.getSql("UserRelation.getUserRelationsBySourceUsername");

        return UserRelation.dao.find(sql, sourceUsername);
    }

    public List<UserRelation> getUserRelationsByTargetUsername(String targetUsername) {
        String sql = Db.getSql("UserRelation.getUserRelationsByTargetUsername");

        return UserRelation.dao.find(sql, targetUsername);
    }

    public List<UserRelation> getUserRelationsByTargetCompanyId(String targetCompanyId) {
        String sql = Db.getSql("UserRelation.getUserRelationsByTargetCompanyId");

        return UserRelation.dao.find(sql, targetCompanyId);
    }

    public UserRelation getUserRelation(String sourceUsername, String targetUsername, int type) {
        String sql = Db.getSql("UserRelation.getUserRelationBySourceUsernameAndTargetUsernameWithType");

        return UserRelation.dao.findFirst(sql, sourceUsername, targetUsername, type, 3);
    }

    /**
     * 通过username和primaryDirectory以及用户关系类型获取用户关系数
     *
     * @param username         用户名
     * @param primaryDirectory 一级目录
     * @param type             用户关系类型 查询client(1,3)时type输入1,查询giver(2,4)时type输入2
     * @return 用户关系
     */
    public Long getUserRelationsCountByUsernameAndPrimaryDirectory(
            String username,
            String primaryDirectory,
            int type) {
        String sql = Db.getSql("UserRelation.getUserRelationsCountByUsernameAndPrimaryDirectory");

        return Db.queryLong(sql, username, primaryDirectory, type, 3);
    }

    public Long getUserRelationsCountByUsernameAndDirectory(
            String username,
            String primaryDirectory,
            String secondaryDirectory,
            int type) {
        String sql = Db.getSql("UserRelation.getUserRelationsCountByUsernameAndDirectory");

        return Db.queryLong(sql, username, primaryDirectory, secondaryDirectory, type, 3);
    }

    public boolean updateUserRelationsPrimaryDirectory(
            String username,
            String oldPrimaryDirectory,
            String newPrimaryDirectory,
            int type) {
        String sql = Db.getSql("UserRelation.getUserRelationsByUsernameAndPrimaryDirectory");

        List<UserRelation> relations = UserRelation.dao.find(sql, username, oldPrimaryDirectory, type, 3);

        for (UserRelation relation : relations) {
            relation.setPrimaryDirectory(newPrimaryDirectory);
            if (!relation.update()) return false;
        }

        return true;
    }

    public boolean updateUserRelationsDirectory(
            String username,
            String oldPrimaryDirectory,
            String oldSecondaryDirectory,
            String newSecondaryDirectory,
            int type) {
        String sql = Db.getSql("UserRelation.getUserRelationsByUsernameAndDirectory");

        List<UserRelation> relations = UserRelation.dao.find(sql, username, oldPrimaryDirectory, oldSecondaryDirectory, type, 3);

        for (UserRelation relation : relations) {
            relation.setSecondaryDirectory(newSecondaryDirectory);
            if (!relation.update()) return false;
        }

        return true;
    }
}
